*Use combined international survey dataset
clear

import delimited Trust_trends_rep.csv

*Code democracy classification, whether a country was coded as an electoral or liberal democracy by the VDem RoW measure in a majority of the years in which it's included in our data
*cap drop vdem_row_perc
*cap drop vdem_row_maj
*cap drop vdem_row_bin
*recode row_vdem (0 1=0) (2 3=1), gen(vdem_row_bin)
*sort country year
*by country, sort: egen vdem_row_perc=mean(vdem_row_bin)
*recode vdem_row_perc (min/0.5=0) (0.51/1=1), gen(vdem_row_maj)
*replace vdem_row_maj=1 if country=="Andorra" | country=="Belize"

**Identifier variables for models
*Recode scale and wording variables so that missing observations take the value 0, so they are not dropped from the analysis
cap tostring q_scale_parl, replace
cap tostring q_scale_gov, replace
cap tostring q_scale_polpar, replace
cap tostring q_scale_leg, replace
cap tostring q_scale_civil, replace
cap tostring q_scale_police, replace

replace q_scale_parl="0" if q_scale_parl=="."
replace q_scale_gov="0" if q_scale_gov=="."
replace q_scale_polpar="0" if q_scale_polpar=="."
replace q_scale_leg="0" if q_scale_leg=="."
replace q_scale_civil="0" if q_scale_leg=="."
replace q_scale_police="0" if q_scale_leg=="."

*Move Australia and New Zealand from the WE/NA region to the Asia & Pacific region
*replace regpol6=6 if country=="Australia" | country=="New Zealand"

**Create "item" variable for each combination of survey source and response scale, for each type of support variable
*Make identifier variables string variables
tostring q_scale_parl q_scale_gov q_scale_polpar q_scale_leg q_scale_civil q_scale_police, replace

cap drop item_parl item_gov item_polpar
cap gen item_parl=study+"_"+q_scale_parl
cap gen item_gov=study+"_"+q_scale_gov
cap gen item_polpar=study+"_"+q_scale_polpar

cap drop item_leg item_civil item_police
cap gen item_leg=study+"_"+q_scale_leg
cap gen item_civil=study+"_"+q_scale_civil
cap gen item_police=study+"_"+q_scale_police

*Keep relevant variables and cases that are non-missing on any of the trust response variables
keep (year country study study_all regpol6 trust_* item_* q* ncases*)

keep if !missing(trust_polpar) | !missing(trust_parl) | !missing(trust_gov) | !missing(trust_leg) | !missing(trust_police) | !missing(trust_civil)

drop if year>2019

order year country study study_all regpol6 trust_* item_* q* ncases*

save "trends_bayes.dta", replace

**Split into separate datasets, one for each type of support variable and rename for Bayesian analysis
*Trust in parliament
use "trends_bayes.dta", clear

keep if !missing(trust_parl)
keep year country study regpol6 trust_parl q_scale_parl item_parl ncases_parl
rename year Year
rename country Country
rename study Project
rename ncases_parl Sample
rename item_parl Item
rename trust_parl Response

gen type="parl"
gen RespN=round(Response*Sample)

order Country Year Sample Item Response Project RespN

save "trends_parl.dta", replace
export delimited using "trends_parl.csv", replace

*Trust in government
use "trends_bayes.dta", clear

keep if !missing(trust_gov)
keep year country study regpol6 trust_gov q_scale_gov item_gov ncases_gov
rename year Year
rename country Country
rename study Project
rename ncases_gov Sample
rename item_gov Item
rename trust_gov Response

gen type="gov"
gen RespN=round(Response*Sample)

order Country Year Sample Item Response Project RespN

save "trends_gov.dta", replace
export delimited using "trends_gov.csv", replace

*Political parties
use "trends_bayes.dta", clear

keep if !missing(trust_polpar)
keep year country study regpol6 trust_polpar q_scale_polpar item_polpar ncases_polpar
rename year Year
rename country Country
rename study Project
rename ncases_polpar Sample
rename item_polpar Item
rename trust_polpar Response

gen type="polpar"
gen RespN=round(Response*Sample)

order Country Year Sample Item Response Project RespN

save "trends_polpar.dta", replace
export delimited using "trends_polpar.csv", replace

*Trust in courts / legal system / justice system
use "trends_bayes.dta", clear

keep if !missing(trust_leg)
keep year country study regpol6 trust_leg q_scale_leg item_leg ncases_leg
rename year Year
rename country Country
rename study Project
rename ncases_leg Sample
gen Item=item_leg
rename trust_leg Response

gen type="leg"
gen RespN=round(Response*Sample)

order Country Year Sample Item Response Project RespN

save "trends_leg.dta", replace
export delimited using "trends_leg.csv", replace

*Trust in police
use "trends_bayes.dta", clear

keep if !missing(trust_police)
keep year country study regpol6 trust_police q_scale_parl item_police ncases_police
rename year Year
rename country Country
rename study Project
rename ncases_police Sample
gen Item=item_police
rename trust_police Response

gen type="police"
gen RespN=round(Response*Sample)

order Country Year Sample Item Response Project RespN

save "trends_police.dta", replace
export delimited using "trends_police.csv", replace

*Trust in civil service
use "trends_bayes.dta", clear

keep if !missing(trust_civil)
keep year country study regpol6 trust_civil q_scale_civil item_civil ncases_civil
rename year Year
rename country Country
rename study Project
rename ncases_civil Sample
gen Item=item_civil
rename trust_civil Response

gen type="civil"
gen RespN=round(Response*Sample)

order Country Year Sample Item Response Project RespN

save "trends_civil.dta", replace
export delimited using "trends_civil.csv", replace

**Combine into one long dataset
use "trends_parl.dta"
append using "trends_gov.dta"
append using "trends_polpar.dta"
append using "trends_leg.dta"
append using "trends_police.dta"
append using "trends_civil.dta"

save "trends_trust.dta", replace
export delimited using "trends_trust.csv", replace

*All trust in representative institutions
use "trends_trust.dta", clear
drop if type=="leg" | type=="police" | type=="civil"

save "trends_trust_rep.dta", replace
export delimited using "trends_trust_rep.csv", replace

**All trust in order institutions
use "trends_trust.dta", clear
drop if type=="parl" | type=="gov" | type=="polpar"

save "trends_trust_order.dta", replace
export delimited using "trends_trust_order.csv", replace
